//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Web;
using System.Web.Configuration;
using WCSFContrib.PageFlow.Utils;

namespace WCSFContrib.PageFlow
{
	/// <summary>
	/// An <see cref="IHttpModule"/> for working with PageFlows
	/// </summary>
	public class PageFlowHttpModule : IHttpModule
	{
		/// <summary>
		/// Creates an instance of the PageFlowHttpModule.
		/// </summary>
		public PageFlowHttpModule()
		{
		}

		/// <summary>
		/// Disposes of the PageFlowHttpModule.
		/// </summary>
		public void Dispose()
		{
		}

		/// <summary>
		/// Initializes the PageFlowHttpModule to work with the <see cref="HttpApplication"/>.
		/// </summary>
		/// <param name="context">The <see cref="HttpApplication"/>.</param>
		public void Init(HttpApplication context)
		{
			if (context == null)
				throw new ArgumentNullException("context");

			context.PostAcquireRequestState += new EventHandler(OnPostAcquireRequestState);

			WebConfigurationManager.OpenWebConfiguration(null);
			HttpRequestHelper.LoadExtensionsHandledByPageHandlerFactoryFromConfig((HttpHandlersSection)WebConfigurationManager.GetSection("system.web/httpHandlers"));
		}

		/// <summary>
		/// Processes an <see cref="HttpRequest"/>.
		/// </summary>
		/// <param name="request">The request to process</param>
		/// <returns>A <see cref="ProcessResult"/>.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "Validation done by Guard class.")]
        public ProcessResult ProcessRequest(HttpRequest request)
		{
			Guard.ArgumentNotNull(request, "request");

			ProcessResult result = new ProcessResult();

			if (HttpRequestHelper.IsHandledByPageHandlerFactory(request.RawUrl))
			{
				result = PageFlowDirectory.Provider.ProcessRequest(request.AppRelativeCurrentExecutionFilePath);
			}
			return result;
		}

		private void OnPostAcquireRequestState(object sender, EventArgs e)
		{
			ProcessResult result = ProcessRequest(HttpContext.Current.Request);
			if (result.ShouldRedirect)
			{
				HttpContext.Current.Response.Redirect(result.RedirectUrl);
			}
		}
	}
}
